package BackTrackingPackage2;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Lzm
 * @version 1.0
 */
public class permute_ {
  List<Integer> list = new ArrayList<>();
  List<List<Integer>> result = new ArrayList<>();
  @Test
  public void permuteTest(){
    int[] nums = new int[]{1,2,3};
    List<List<Integer>> permute = permute(nums);
    for (List<Integer> integers : permute) {
      System.out.println(integers.toString());
    }
  }
  public List<List<Integer>> permute(int[] nums) {
    backTracking(nums,0);
    return result;
  }
  public void backTracking(int[] nums,int startIndex){
    if (list.size() == nums.length){
      result.add(new ArrayList<>(list));
      return;
    }
    for (int i = startIndex ; i < nums.length ; i++){
      if (i > 0 && nums[i] == nums[i - 1]){
        continue;
      }
      else {
        list.add(nums[i]);
        backTracking(nums,i+1);
        list.remove(list.size() - 1);
      }
    }
  }
}
